package stack;



/**
 * @Program: dataOJ->CalculateDemo
 * @Description: 中缀表达式计算器
 * @Author: Tansty
 * @Date: 2022-02-11 20:43
 * @Version： 1.0
 **/

public class CalculateDemo {
    public int calculate(String regix){
        ArrayStack2 numberStack = new ArrayStack2(10);
        ArrayStack2 operatorStack = new ArrayStack2(10);
        char[] numbers = regix.toCharArray();
        int index = 0;
        char ch ;
        String num = "";
        while (true){
            ch = numbers[index];
            //是操作符
           if(operatorStack.isOperator(ch)){
               //栈为空直接放进去
               if(operatorStack.isNull()){
                   operatorStack.push(ch);
               }else{
                   //判断在栈顶的优先级是大于等于当前符号的优先级
                   if(operatorStack.getPriority(operatorStack.peek())>=operatorStack.getPriority(ch)){
                       //取出数字栈中的两个数进行计算并放回数字栈中
                       int num1 = numberStack.pop();
                       int num2 = numberStack.pop();
                       char opr = (char) operatorStack.pop();
                       int result = operatorStack.calculate(num1, num2, opr);
                       numberStack.push(result);
                       operatorStack.push(ch);
                   }else{
                       operatorStack.push(ch);
                   }
               }
           }else{
                //判断是否是最后一位
                if(index==numbers.length-1){
                    num += ch;
                    numberStack.push(Integer.parseInt(num));
                    num = "";
                }else{
                    //判断后面一位是否是操作符
                    if(!operatorStack.isOperator(numbers[index+1])){
                        num += ch;
                    }else{
                        num += ch;
                        numberStack.push(Integer.parseInt(num));
                        num = "";
                    }
                }
           }
           if(index==numbers.length-1){
               while (true){
                   int num1 = numberStack.pop();
                   int num2 = numberStack.pop();
                   char operator = (char) operatorStack.pop();
                   int res = operatorStack.calculate(num1, num2, operator);
                   numberStack.push(res);
                   if(operatorStack.isNull()){
                       break;
                   }
               }
               break;
           }else{
               index++;
           }
        }
        return numberStack.pop();
    }

    public static void main(String[] args) {
        String regix = "50*4+13";
        int result = new CalculateDemo().calculate(regix);
        System.out.println(result);
    }

}
class ArrayStack2{
    private int top = -1;
    private int[] array = null;
    private int maxSize = 0;

    public ArrayStack2(int maxSize) {
        this.maxSize = maxSize;
        array = new int[maxSize];
    }

    public boolean isNull() {
        return this.top == -1;
    }

    public boolean isFull() {
        return this.top >= this.maxSize - 1;
    }
    public boolean isOperator(char opr){
        return opr == '+' || opr == '-' || opr == '*' || opr == '/';
    }
    public int getPriority(char ch){
        if(ch == '*'||ch=='/'){
            return 2;
        }else if(ch=='+'||ch=='-'){
            return 1;
        }else {
            return 0;
        }
    }
    public int calculate(int num1,int num2,char opr){
        int result = 0;
        switch (opr){
            case '+':
                result = num1+num2;
                break;
            case '-' :
                result = num2-num1;
                break;
            case '*':
                result = num2*num1;
                break;
            case '/':
                result = num2/num1;
                break;
            default:
                break;
        }
        return result;
    }
    public char peek(){
        return (char) array[top];
    }
    public void list() {
        for (int i = 0; i <= top; i++) {
            System.out.println(array[i]);
        }
    }
    public void push(int value) {
        if (isFull()) {
            throw new RuntimeException("栈已满");
        }
        array[++top] = value;
    }
    public int pop() {
        if (isNull()) {
            throw new RuntimeException("栈为空");
        }
        return array[top--];
    }
}
